package com.feng.behavior
{
	import com.feng.data.StateData;
	import com.feng.debug.logger;
	import com.feng.utils.PlayerUtils;

	/**
	 * 攻击行为
	 * @author 风之守望者
	 * 2013-3-27
	 */
	public class AttackBehavior
	{
		/**攻击欲望 表	值越大攻击欲望越大 */
		public var attckDesireTable:Array;

		private var stateData:StateData;

		public function AttackBehavior(stateData:StateData)
		{
			this.stateData = stateData;
		}

		private function get enemyList():Array
		{
			return stateData.enemyList;
		}

		private function get me():Object
		{
			return stateData.me;
		}

		/**
		 * 增加攻击欲望
		 * @param playerId 目标玩家编号
		 * @param value 攻击欲望 值
		 */
		public function addAttckDesire(playerId:int, value:int):void
		{
			if (value == 0)
				return;
			logger("对玩家" + playerId + "增加仇恨" + value);
			attckDesireTable[playerId] += value;
		}

		/** 抢人头	有便宜一定要捡啊！ */
		public function kill_steal(priority:Number):void
		{
			if (stateData.currentSpeedDiePlayers.length > 0)
			{
				addAttckDesire(stateData.currentSpeedDiePlayers[0], priority);
				return;
			}

			var target:Object;
			var attacker:Object;
			var attackerId:int;
			var myDamage:int;
			//如果我能够杀死
			for each (target in stateData.enemyList)
			{
				if (target.id != me.id)
				{
					myDamage = PlayerUtils.attackDamage(me, target) * stateData.getAttackNum(me.id);
					if (stateData.realState[target.id].hp <= myDamage)
					{
						addAttckDesire(target.id, priority);
						return;
					}
				}
			}

			//与我同时出手的玩家编号
			var playerIdList:Array = stateData.getSameSpeed();
			for each (target in enemyList)
			{
				for each (attackerId in playerIdList)
				{
					attacker = stateData.state[attackerId];
					if (attacker)
					{
						var damage:Number = PlayerUtils.attackDamage(attacker, target) * stateData.getAttackNum(attacker.id);
						//判断是否能够击毙该玩家
						if (target.hp <= damage)
						{
							if (target.id != attacker.id)
							{
								addAttckDesire(target.id, priority);
							}
							//有木有人会自杀啊？
							if (target.id == attacker.id)
							{
								for each (var attacter2:Object in stateData.livingPlayers)
								{
									if (attacter2.id != attacker.id)
									{
										//假如有玩家能够杀死自己，判定为可能自杀
										if (attacker.hp <= PlayerUtils.attackDamage(attacter2, attacker))
										{
											addAttckDesire(target.id, priority / 2);
										}
									}
								}
							}
						}
					}
				}
			}
		}

		public function burang_rentou(priority:Number):void
		{
			for each (var target:Object in stateData.enemyList)
			{
				var damage:int = PlayerUtils.attackDamage(stateData.me, target);
				if (target.hp > damage)
				{
					for each (var attacker:Object in stateData.enemyList)
					{
						var damage1:int = PlayerUtils.attackDamage(attacker, target);
						if (attacker.spd == stateData.speedSortList[0].spd)
							damage1 = damage1 * 2;
						if (stateData.realState[target.id].hp <= damage + damage1)
						{
							addAttckDesire(target.id, priority);
						}
					}
				}
			}
		}

		/**
		 *  三人模式攻击
		 */
		public function attact_three_strategy(priority:Number):void
		{

		}

		/** 四人模式攻击 */
		public function attact_four_strategy(priority:Number):void
		{
			var attackedNum:int;
			//计算最容易丢人头 与 最能拿到人头的玩家
			var maxLostheadPlayers:Array = stateData.getMaxLostheadPlayers();
			var maxKillStealPlayers:Array = stateData.getMaxKillStealPlayers(maxLostheadPlayers[0].id);

			var adsIndex:int = maxKillStealPlayers.length > 2 ? 2 : (maxKillStealPlayers.length - 1);
			//如果我最能拿到人头
			if (maxKillStealPlayers[0].id == stateData.me.id //
				&& maxKillStealPlayers[0].attackNum != maxKillStealPlayers[adsIndex].attackNum //
				&& maxKillStealPlayers[0].attackNum <= 8)
			{
				addAttckDesire(maxLostheadPlayers[0].id, priority);
				return;
			}

			//攻击最能抢到人头的
			if (maxKillStealPlayers[0].attackNum != maxKillStealPlayers[adsIndex].attackNum)
			{
				for (var j:int = 0; j < maxKillStealPlayers.length && j < 2; j++)
				{
					attackedNum = stateData.preTurnAttackedNum(maxKillStealPlayers[j].id);
					if (attackedNum > 0 && maxKillStealPlayers[j].id != stateData.me.id)
					{
						addAttckDesire(maxKillStealPlayers[j].id, priority);
						return;
					}
				}
			}

			if (stateData.me.spd == stateData.speedSortList[0].spd //
				|| stateData.me.spd == stateData.speedSortList[1].spd)
			{
				if (stateData.speedSortList[1].spd + 3 >= stateData.speedSortList[0].spd)
				{
					if (stateData.me.id == stateData.speedSortList[0].id)
						addAttckDesire(stateData.speedSortList[1].id, priority);
					else
						addAttckDesire(stateData.speedSortList[0].id, priority);
					return;
				}
			}

			//攻击攻击力最高的
			if (stateData.attackSortList[0].atk != stateData.attackSortList[1].atk)
			{
				for (var i:int = 0; i < stateData.attackSortList.length && i < 2; i++)
				{
					attackedNum = stateData.preTurnAttackedNum(stateData.attackSortList[i].id);
					if (attackedNum > 0 && stateData.attackSortList[i].id != stateData.me.id)
					{
						addAttckDesire(stateData.attackSortList[i].id, priority);
						return;
					}
				}
			}

			//攻击被攻击最多的玩家
			var perTurnAttackedSort:Array = stateData.perTurnAttackedSort();
			for (var k:int = 0; k < perTurnAttackedSort.length; k++)
			{
				if (stateData.state[perTurnAttackedSort[k].targetId].hp > 40)
				{
					addAttckDesire(perTurnAttackedSort[k].targetId, priority);
					return;
				}
			}

			//没办法了，寻找不到攻击目标
			addAttckDesire(stateData.enemyList[int(Math.random() * stateData.enemyList.length)].id, priority);
		}

		/** 五人模式攻击 */
		public function attact_five_strategy(priority:Number):void
		{
			if (stateData.me.spd == stateData.speedSortList[0].spd //
				|| stateData.me.spd == stateData.speedSortList[1].spd)
			{
				if (stateData.me.spd - stateData.speedSortList[2].spd > 4)
				{
					if (stateData.me.id == stateData.speedSortList[0].id)
						addAttckDesire(stateData.speedSortList[1].id, priority);
					else
						addAttckDesire(stateData.speedSortList[0].id, priority);
					return;
				}
			}

			var attackedNum:int;
			//计算最容易丢人头 与 最能拿到人头的玩家
			var maxLostheadPlayers:Array = stateData.getMaxLostheadPlayers();
			var maxKillStealPlayers:Array = stateData.getMaxKillStealPlayers(maxLostheadPlayers[0].id);

			var adsIndex:int = maxKillStealPlayers.length > 2 ? 2 : (maxKillStealPlayers.length - 1);
			//如果我最能拿到人头
			if (maxKillStealPlayers[0].id == stateData.me.id //
				&& maxKillStealPlayers[0].attackNum != maxKillStealPlayers[adsIndex].attackNum //
				&& maxKillStealPlayers[0].attackNum <= 8)
			{
				addAttckDesire(maxLostheadPlayers[0].id, priority);
				return;
			}

			//攻击最能抢到人头的
			if (maxKillStealPlayers[0].attackNum != maxKillStealPlayers[adsIndex].attackNum)
			{
				for (var j:int = 0; j < maxKillStealPlayers.length && j < 2; j++)
				{
					attackedNum = stateData.preTurnAttackedNum(maxKillStealPlayers[j].id);
					if (attackedNum > 0 && maxKillStealPlayers[j].id != stateData.me.id)
					{
						addAttckDesire(maxKillStealPlayers[j].id, priority);
						return;
					}
				}
			}

			//攻击攻击力最高的
			if (stateData.attackSortList[0].atk != stateData.attackSortList[1].atk)
			{
				for (var i:int = 0; i < stateData.attackSortList.length && i < 2; i++)
				{
					attackedNum = stateData.preTurnAttackedNum(stateData.attackSortList[i].id);
					if (attackedNum > 0 && stateData.attackSortList[i].id != stateData.me.id)
					{
						addAttckDesire(stateData.attackSortList[i].id, priority);
						return;
					}
				}
			}

			//攻击被攻击最多的玩家
			var perTurnAttackedSort:Array = stateData.perTurnAttackedSort();
			for (var k:int = 0; k < perTurnAttackedSort.length; k++)
			{
				if (stateData.state[perTurnAttackedSort[k].targetId].hp > 40)
				{
					addAttckDesire(perTurnAttackedSort[k].targetId, priority);
					return;
				}
			}

			//没办法了，寻找不到攻击目标
			addAttckDesire(stateData.enemyList[int(Math.random() * stateData.enemyList.length)].id, priority);
		}

		/** 六人模式攻击 */
		public function attact_six_strategy(priority:Number):void
		{
			if (stateData.me.spd == stateData.speedSortList[0].spd //
				|| stateData.me.spd == stateData.speedSortList[1].spd)
			{
				if (stateData.me.spd - stateData.speedSortList[3].spd > 4)
				{
					if (stateData.me.id == stateData.speedSortList[0].id)
						addAttckDesire(stateData.speedSortList[1].id, priority);
					else
						addAttckDesire(stateData.speedSortList[0].id, priority);
					return;
				}
			}

			var attackedNum:int;
			//计算最容易丢人头 与 最能拿到人头的玩家
			var maxLostheadPlayers:Array = stateData.getMaxLostheadPlayers();
			var maxKillStealPlayers:Array = stateData.getMaxKillStealPlayers(maxLostheadPlayers[0].id);

			var adsIndex:int = maxKillStealPlayers.length > 2 ? 2 : (maxKillStealPlayers.length - 1);
			//如果我最能拿到人头
			if (maxKillStealPlayers[0].id == stateData.me.id //
				&& maxKillStealPlayers[0].attackNum != maxKillStealPlayers[adsIndex].attackNum //
				&& maxKillStealPlayers[0].attackNum <= 8)
			{
				addAttckDesire(maxLostheadPlayers[0].id, priority);
				return;
			}

			//攻击最能抢到人头的
			if (maxKillStealPlayers[0].attackNum != maxKillStealPlayers[adsIndex].attackNum)
			{
				for (var j:int = 0; j < maxKillStealPlayers.length && j < 2; j++)
				{
					attackedNum = stateData.preTurnAttackedNum(maxKillStealPlayers[j].id);
					if (attackedNum > 0 && maxKillStealPlayers[j].id != stateData.me.id)
					{
						addAttckDesire(maxKillStealPlayers[j].id, priority);
						return;
					}
				}
			}

			//攻击攻击力最高的
			if (stateData.attackSortList[0].atk != stateData.attackSortList[1].atk)
			{
				for (var i:int = 0; i < stateData.attackSortList.length && i < 2; i++)
				{
					attackedNum = stateData.preTurnAttackedNum(stateData.attackSortList[i].id);
					if (attackedNum > 0 && stateData.attackSortList[i].id != stateData.me.id)
					{
						addAttckDesire(stateData.attackSortList[i].id, priority);
						return;
					}
				}
			}

			//攻击被攻击最多的玩家
			var perTurnAttackedSort:Array = stateData.perTurnAttackedSort();
			for (var k:int = 0; k < perTurnAttackedSort.length; k++)
			{
				if (stateData.state[perTurnAttackedSort[k].targetId].hp > 40)
				{
					addAttckDesire(perTurnAttackedSort[k].targetId, priority);
					return;
				}
			}

			//没办法了，寻找不到攻击目标
			addAttckDesire(stateData.enemyList[int(Math.random() * stateData.enemyList.length)].id, priority);
		}
	}
}
